package com.eightbears.beboy.adapter;

import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.entity.HongBaoEntity;

public class MoneyPackageAdapter extends BaseQuickAdapter<HongBaoEntity.HongBao, BaseViewHolder> {

    private double money;
    private String resultInfo;
    private TextView iv_money, tv_date, tv_no_use, tv_condition, tv_sign, tv_result_title;

    public MoneyPackageAdapter() {
        super(R.layout.item_money_package, null);
    }

    public MoneyPackageAdapter(double money) {
        super(R.layout.item_money_package, null);
        this.money = money;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, HongBaoEntity.HongBao hongBaoEntity) {
        double limitMoney = Double.valueOf(hongBaoEntity.getLimit_money());
        double redMoney = Double.valueOf(hongBaoEntity.getMoney());
        long date = Long.valueOf(hongBaoEntity.getEnd_at());
        baseViewHolder.setText(R.id.iv_money, hongBaoEntity.getMoney())
                .setText(R.id.tv_date, CommonUtils.TimeStamp2Date(hongBaoEntity.getEnd_at(), "yyyy-MM-dd") + " 到期")
                .setText(R.id.tv_no_use, "消费" + hongBaoEntity.getLimit_money() + "可用")
                .setText(R.id.tv_condition, "消费" + hongBaoEntity.getLimit_money() + "可用");

        iv_money = baseViewHolder.getView(R.id.iv_money);
        tv_date = baseViewHolder.getView(R.id.tv_date);
        tv_no_use = baseViewHolder.getView(R.id.tv_no_use);
        tv_condition = baseViewHolder.getView(R.id.tv_condition);
        tv_sign = baseViewHolder.getView(R.id.tv_sign);
        tv_result_title = baseViewHolder.getView(R.id.tv_result_title);
        if (money != 0) {
            if (money < limitMoney) {
                resultInfo = "消费满" + limitMoney + "可用";
                unSelect();
                return;
            }

            if (money < redMoney) {
                resultInfo = "消费金额不能小于红包金额";
                unSelect();
                return;
            }

            if (System.currentTimeMillis() / 1000 > date) {
                resultInfo = "已过使用期限";
                unSelect();
                return;
            }

            useSelect();
            baseViewHolder.addOnClickListener(R.id.ll_item);
        }
    }


    private void unSelect() {
        iv_money.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_date.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_no_use.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_condition.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_sign.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_result_title.setVisibility(View.VISIBLE);
        tv_no_use.setText(resultInfo);
    }

    private void useSelect() {
        iv_money.setTextColor(mContext.getResources().getColor(R.color.text_red_package_money));
        tv_sign.setTextColor(mContext.getResources().getColor(R.color.text_red_package_money));
        tv_date.setTextColor(mContext.getResources().getColor(R.color.text_main_color));
        tv_no_use.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_condition.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_result_title.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        tv_result_title.setText("使用条件");
        tv_result_title.setVisibility(View.VISIBLE);
    }
}